﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using Tesla.Graphics;
using Tesla.Graphics.Implementation;
using Tesla.Xna.Util;
using XFG = Microsoft.Xna.Framework.Graphics;

namespace Tesla.Xna.Graphics.Implementation {
    /// <summary>
    /// Concrete XNA 4.0 implementation for <see cref="SamplerState"/>.
    /// </summary>
    public sealed class XNASamplerStateImplementation : SamplerStateImplementation {
        private XFG.SamplerState _samplerState;

        /// <summary>
        /// Gets the actual XNA SamplerState object.
        /// </summary>
        internal XFG.SamplerState XNASamplerState {
            get {
                return _samplerState;
            }
        }

        /// <summary>
        /// Creates a new instance of <see cref="XNASamplerStateImplementation"/>.
        /// </summary>
        internal XNASamplerStateImplementation() : base() {}

        /// <summary>
        /// Releases unmanaged resources and performs other cleanup operations before the
        /// <see cref="XNASamplerStateImplementation"/> is reclaimed by garbage collection.
        /// </summary>
        ~XNASamplerStateImplementation() {
            Dispose(false);
        }

        /// <summary>
        /// Binds the implementation. This is called the first time an unbound state is set to the device or manually by the user in order to
        /// create the underlying state ahead of time (best practice). Once called the state properties are read-only.
        /// </summary>
        public override void BindSamplerState() {
            if(!base.IsBound) {
                _samplerState = new XFG.SamplerState();
                _samplerState.AddressU = XNAHelper.ToXNATextureAddressMode(base.AddressU);
                _samplerState.AddressV = XNAHelper.ToXNATextureAddressMode(base.AddressV);
                _samplerState.AddressW = XNAHelper.ToXNATextureAddressMode(base.AddressW);
                _samplerState.Filter = XNAHelper.ToXNATextureFilter(base.Filter);
                _samplerState.MaxAnisotropy = base.MaxAnisotropy;
                _samplerState.MipMapLevelOfDetailBias = base.MipMapLevelOfDetailBias;
                base.IsBound = true;
            }
        }
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing) {
            if(!IsDisposed) {
                //Dispose of managed resources
                if(disposing) {
                    if(_samplerState != null) {
                        _samplerState.Dispose();
                    }
                }
            }
            base.Dispose(disposing);
        }
    }
}
